function updateCoords(c) {
	$('#img_crop_x').val(c.x);
	$('#img_crop_y').val(c.y);
	$('#img_crop_w').val(c.w);
	$('#img_crop_h').val(c.h);
};

function doPreview(coords) {
	if (parseInt(coords.w) > 0) {
		var rx = 75 / coords.w;
		var ry = 75 / coords.h;
		$('#crop_img_thumb').css({
			width: Math.round(rx * imgwidth) + 'px',
			height: Math.round(ry * imgheight) + 'px',
			marginLeft: '-' + Math.round(rx * coords.x) + 'px',
			marginTop: '-' + Math.round(ry * coords.y) + 'px'
		});
	}
};
function resetCrop() {
	$("#crop_img_orig").remove();
	$("#crop_img_thumb").remove();
	$(".jcrop-holder").Jcrop("destroy");
	$(".jcrop-holder").remove();
}
function crop(response, crop) {
	if (response.filepath != null && response.filepath != '') {
		var scale_x = response.scale_x;
		var scale_y = response.scale_y;
		var newImg = new Image();
		$(newImg).load().attr('src', siteRoot+response.filepath+'?'+Math.floor(Math.random()*1001))
						.attr('id', 'crop_img_orig');
		imgwidth = response.imagewidth;
		imgheight = response.imageheight;
		$('#crop_container').append(newImg);
		
		if (crop) {
			var aspectRatio = scale_x/scale_y;
			$(newImg).Jcrop({
				aspectRatio: aspectRatio,
				setSelect: [0, 0, scale_x, scale_y],
				//onChange: doPreview,
				//onSelect: doPreview,
				onSelect: updateCoords
			});
			tempImagePath = response.filepath;
		}
	}
};